<?php


namespace app\common\model\mysql;


class Goods extends BaseModel
{
    /**
     * 单图片域名格式化
     * @param $value
     * @return string
     */
    public function getImageAttr($value) {
        return request()->domain().$value;
    }

    /**
     * 多图片域名格式化
     * @param $value
     * @return array
     */
    public function getCarouselImageAttr($value) {
        if(!empty($value)) {
            $value = explode(",", $value);
            $value = array_map(function($v){
                return request()->domain() . $v;
            }, $value);
        }
        return $value;
    }

    /**
     * 获取首页轮播大图
     * @param $where
     * @param $num
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRotationChart($where, $num) {
        $order = ["list_order" => "desc", "id" => "desc"];
        $list = $this->field('id, title, big_image as image, sku_id')
            ->where($where)
            ->order($order)
            ->limit($num)
            ->select();
        return $list;
    }
    /**
     * 分页获取商品列表
     * @param $filed
     * @param $where
     * @param $num
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getLists($filed, $where, $num) {
        $order = ["list_order" => "desc", "id" => "desc"];
        $list = $this->field($filed)->where($where)
            ->order($order)
            ->paginate($num);
        //echo $this->getLastSql();die;
        return $list;
    }

    /**
     * 添加商品  返回主键ID
     * @param $data
     * @return mixed
     */
    public function addReturnId($data) {
        $this->save($data);
        return $this->id;
    }

    /**
     * 根据ID 更新数据
     * @param $id
     * @param $updateData
     * @return Goods|bool
     */
    public function updateById($id, $updateData) {
        return $this->where('id', $id)->update($updateData);
    }

    /**
     * 获取分类下的所有商品 包括子类的
     * @param $categoryIds
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoodsByCategoryIds($categoryIds, $limit=0) {
        $order = ["list_order" => "desc", "id" => "desc"];
        if ($limit) {
            return $this->field('id, sku_id, title_cut as title,promotion_title, price,recommend_image as image')
                ->where('category_id','in', $categoryIds)
                ->order($order)
                ->limit($limit)
                ->select();
        } else {
            return $this->field('id, title_cut as title, price,recommend_image')
                ->where('category_id','in', $categoryIds)
                ->order($order)
                ->paginate();
        }

    }



    public function getNormalGoods($id) {
        $where = [
            ['status', '=', 1],
            ['id', '=', $id]
        ];
        return $this->where($where)->field('id,description,sku_id,title')->find();
    }
}